package org.shan.code.generator;

import org.shan.code.utils.NameUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生成 service
 */
public class ServiceGenerator {

    // 接口类路径
    public static String interfacePath = new File("").getAbsolutePath();
    // 实现类路径
    public static String realPath = new File("").getAbsolutePath();

    // 指定包名
    public static String basePackage = "org.herui.campus.wushui";

    // 作者名字
    public String authorName = "zzb";
    // 指定需要生成的表的表名，全部生成设置为null
    public String[] generateTables;


    public void generate() throws Exception {

        // 是否有指定生成表，有指定则直接用指定表，没有则全表生成
        List<String> tableNames = new ArrayList<>();
        for (String tableName : generateTables) {
            tableNames.add(tableName);
        }

        PrintWriter pw = null;
        for (int j = 0; j < tableNames.size(); j++) {
            String tableName = tableNames.get(j);

            String content = parseInterface(tableName);
            String dirName = pkgDirName(interfacePath, basePackage + ".service");
            String javaPath = dirName + "/" + getEntityName(tableName) + "Service" + ".java";
            FileWriter fw = new FileWriter(javaPath);
            pw = new PrintWriter(fw);
            pw.println(content);
            pw.flush();
            if (pw != null) {
                pw.close();
            }

            content = parseReal(tableName);
            dirName = pkgDirName(realPath, basePackage + ".service.iml");
            javaPath = dirName + "/" + getEntityName(tableName) + "ServiceIml" + ".java";
            fw = new FileWriter(javaPath);
            pw = new PrintWriter(fw);
            pw.println(content);
            pw.flush();
            if (pw != null) {
                pw.close();
            }
        }

    }

    /**
     * 功能：获取并创建实体所在的路径目录
     * E:\workspace\idea\lis-server\lis_api\base_api\src\main\java\org\shan\lis\device\entity
     *
     * @return
     */
    private String pkgDirName(String basePath, String packageOutPath) {
        String dirName = basePath + "\\src\\main\\java\\" + packageOutPath.replace(".", "\\");
        File dir = new File(dirName);
        if (!dir.exists()) {
            dir.mkdirs();
            System.out.println("mkdirs dir 【" + dirName + "】");
        }
        return dirName;
    }

    /**
     * @description 生成class的所有内容
     */
    private String parseInterface(String tableName) {

        String entityName = getEntityName(tableName);

        StringBuffer sb = new StringBuffer();
        sb.append("package " + basePackage + ".service;\r\n");
        sb.append("\r\n");
        // 判断是否导入工具包
        sb.append("import com.baomidou.mybatisplus.core.mapper.BaseMapper;\r\n\r\n");
        sb.append("import " + basePackage + ".entity." + entityName + ";\r\n");
        sb.append("import " + basePackage + ".IBaseService" + ";\r\n");

        // 注释部分
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sb.append("/**\r\n");
        sb.append(" * author name: " + authorName + "\r\n");
        sb.append(" * create time: " + format.format(new Date()) + "\r\n");
        sb.append(" */ \r\n");

        // 注解部分

        // 实体部分
        String classExtends = "";
        classExtends = " extends IBaseService<" + entityName + ">";

        sb.append("public interface " + entityName + "Service" + classExtends + "{\r\n\r\n");

        sb.append("\r\n");
        sb.append("}\r\n");
        return sb.toString();
    }

    /**
     * @description 生成class的所有内容
     */
    private String parseReal(String tableName) {

        String entityName = getEntityName(tableName);

        StringBuffer sb = new StringBuffer();
        sb.append("package " + basePackage + ".service.iml;\r\n");
        sb.append("\r\n");
        // 判断是否导入工具包
        sb.append(String.format("import %s.dao.mysql.%sMapper;\r\n", basePackage, entityName));
        sb.append(String.format("import %s.entity.%s;\r\n", basePackage, entityName));
        sb.append(String.format("import %s.service.%sService;\r\n", basePackage, entityName));
        sb.append(String.format("import %s.service.BaseService;\r\n", basePackage, entityName));
        sb.append("import org.springframework.beans.factory.annotation.Autowired;\n" + "import org.springframework.stereotype.Service;\r\n");

        // 注释部分
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sb.append("/**\r\n");
        sb.append(" * author name: " + authorName + "\r\n");
        sb.append(" * create time: " + format.format(new Date()) + "\r\n");
        sb.append(" */ \r\n");

        // 注解部分
        sb.append("@Service\r\n");

        // 实体部分
        String classExtends = "";
        classExtends = String.format("extends BaseService<%sMapper, %s> implements %sService", entityName, entityName, entityName);

        sb.append("public class " + entityName + "ServiceIml " + classExtends + "{\r\n\r\n");

        sb.append(String.format(
                "\t@Autowired\n" + "\t@Override\n" + "\tpublic void setBaseMapper(%sMapper baseMapper) {\n" + "\t\tsuper.setBaseMapper(baseMapper);"
                + "\n" + "\t}",
                entityName));

        sb.append("\r\n");
        sb.append("}\r\n");
        return sb.toString();
    }

    private String getEntityName(String tableName) {
        return NameUtils.under2camel(tableName.substring(2), true);
    }
}
